import React from 'react';
import { Form, Input,InputNumber, Button, Checkbox,message } from 'antd';
const FormItem = Form.Item;
import { login,_validate, logout } from '../store/actions';

function noop() {
    return false;
}
class _FormLogin extends React.Component{
    constructor(props){
        super(props);
        this.handleSubmit = this.handleSubmit.bind(this)
    }
    static defaultProps = {
        autoPlay: false,
        maxLoops: 10
    };
    handleSubmit(e) {
        e.preventDefault();
        const _this = this;
        _validate(e,_this,function(values){
            _this.props.dispatch(login(values,function(res){
                if(res.content.user.type == 1){
                    if(values.username === 'systemadmin'){
                        message.success('管理员-登录成功',1);
                        _this.props._r.push('/_admin');
                    }else{
                        message.success('登录成功',1);
                        _this.props._r.push('/images');
                    }
                }else if(res.content.user.type == 2){
                    const ispass = res.content.user.ispass,
                      specialstatus = res.content.user.specialstatus,
                      status = res.content.user.status,
                      token = res.content.accessToken;
                    switch(ispass){
                        case 0://没审核（初始状态）
                          switch(specialstatus){
                              case 0://没填资料
                                  message.warning('请尽快有完善个人资料',1);
                                  _this.props._r.push('/reg-spec-form?step=1');
                                break;
                              case 1://填过资料
                              default:
                                  message.info('正在审核您的申请，请耐心等待',1);
                                  _this.props._r.push('/reg-spec-form?step=2&status=0');
                                  break;
                          }
                          break;
                        case 1://通过
                          switch (status){
                              case 1://启用
                                  message.success('专家-登录成功',1);
                                  _this.props._r.push('/work-board');
                              break;
                              case 0://停用
                              default:
                                  message.error('您的账号已被冻结，请联系您的管理员！',2);
                                  _this.props.dispatch(logout());
                                  break;
                              break;
                          }
                        break;
                        case 2://拒绝
                            message.error('您的申请被拒绝了，请联系您的管理员！',2);
                            _this.props._r.push('/reg-spec-form?step=2&status=1');
                        break;
                    }
                }
            },function(res){
                message.error(res.head.msg,1)
            }));
        });
    }
    render(){
        const { getFieldProps, getFieldError, isFieldValidating } = this.props.form;
        const nameProps = getFieldProps('username', {
            rules: [
                { required: true, min: 5, message: '请填写正确的用户名' }
            ]
        });
        const passwdProps = getFieldProps('password', {
            rules: [
                { required: true, whitespace: true, message: '请填写密码' },
                { validator: this.checkPass }
            ]
        });
        const formItemLayout = {
            wrapperCol: { span: 18,offset:3 }
        };
        const _this = this;
        return (
            <div>
                <h2 id="accessFormTitle">登录</h2>
                <Form horizontal form={this.props.form} onSubmit={this.handleSubmit}>
                    <FormItem
                        {...formItemLayout}
                    >
                        <Input {...nameProps} placeholder="请输入用户名" style={{width:'100%'}} />
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                    >
                        <Input {...passwdProps}
                            type="password"
                            placeholder="请输入密码"
                            autoComplete="off"
                            onContextMenu={noop}
                            onPaste={noop}
                            onCopy={noop}
                            onCut={noop}
                        />
                    </FormItem>
                    <FormItem {...formItemLayout}>
                        <Checkbox {...getFieldProps('agreement')}>记住我</Checkbox><br />
                        <Button style={{
                            display:'block',
                            width: '100%',
                            height: '44px',
                            fontSize: '16px'
                        }}
                                type="primary"
                                htmlType="submit"
                                onClick={this.handleSubmit}
                                size="large"
                        >登录</Button>

                        <div style={{overflow:'hidden'}}>
                            <a style={{float:'right'}} onClick={function(){
                                _this.props._r.push('/register');
                            }} >免费注册</a>
                            <a style={{float:'left',color:'#333'}} onClick={function(){
                                _this.props._r.push('/mail-reset-pass');
                            }} >忘记密码？点这里</a>
                        </div>
                    </FormItem>
                </Form>
            </div>
        )
    }
}

const FormLogin = Form.create()(_FormLogin);
export default FormLogin